package fr.emse.tatiana.cordtravisualisation;

import org.eclipse.draw2d.Ellipse;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.RoundedRectangle;
import org.eclipse.draw2d.Shape;
import org.eclipse.draw2d.Triangle;
import org.eclipse.swt.graphics.Color;

import fr.emse.tatiana.graphical.IRuleableFigure;
import fr.emse.tatiana.graphical.Octagon;

public class CordtraRule {

	public static final String VISIBLE = "visible";
	public static final String COLOR = "colour";
	public static final String BORDER_COLOR = "border colour";
	public static final String POSITION = "relative position";
	public static final String HEIGHT = "height";
	public static final String SET_POSITION = "absolute position";
	public static final String SHAPE = "shape";
	public static final String[] properties = {VISIBLE, COLOR, BORDER_COLOR, POSITION, SET_POSITION,  HEIGHT, SHAPE};
	private int currentvalue;
	private String property;
	private String facetvalue;
	private String facetname;

	
	
	public CordtraRule(int currentvalue, String property, String facetvalue,
			String facetname) {
		super();
		this.currentvalue = currentvalue;
		this.property = property;
		this.facetvalue = facetvalue;
		this.facetname = facetname;
	}

	public Color getCurrentColor() {
		int blue = 0x0000ff;
		int red = 0xff0000;
		int green = 0x00ff00;
		return new Color(null, (currentvalue & red)>>16, (currentvalue & green)>>8, currentvalue & blue); // 
		
	}
	public void apply(ReplayableFacet model, IRuleableFigure f) {

		if (!test(model))
			return;



		boolean b = (currentvalue>0);
		int i =  currentvalue;
		Color c =  getCurrentColor();
		if (VISIBLE.equals(property)) {
			f.setVisible(b);
		} else if (COLOR.equals(property)) {
			if (c!=null)
				f.setBackgroundColor(c);
		} else if (BORDER_COLOR.equals(property)) {
			if (c!=null)
				f.setBorderColor(c);
		} else if (HEIGHT.equals(property)) {
			f.setHeight(i);
		} else if (POSITION.equals(property)) {
			f.moveY(i);
		} else if (SET_POSITION.equals(property)) {
			f.setAbsoluteY(i);
		} else if (SHAPE.equals(property)) {
			Shape shape;
			switch (i) {
			case 1:
				shape = new Ellipse();
				break;
			case 2:
				shape = new RectangleFigure();
				break;
			case 3:
				shape = new RoundedRectangle();
				break;
			case 4:
				shape = new Triangle();
				break;
			case 5:
				shape = new Octagon();
				break;
			case 0:
			default:
				shape = null;
			}
			f.setShape(shape);
		}
	}


	private boolean test(ReplayableFacet elem) {
		if (facetname==null && facetvalue==null) {
			return true;
		} else if (facetname==null) {
			for (int i=0; i< elem.replayable.getFacetCount(); i++) {
				if (elem.replayable.getFacetText(elem.event, i).equals(facetvalue)) {
					return true;
				}
			}
		} else if (facetvalue==null) {
			return elem.replayable.getFacetCol(facetname) == elem.facet ; // TODO fix for when facets are not always set
		} else {
			try {
				return (elem.replayable.getFacetCol(facetname)==elem.facet) && (elem.replayable.getFacetText(elem.event, elem.replayable.getFacetCol(facetname)).equals(facetvalue));
			} catch (Exception e) {
				// do nothing - facet is currently out of bounds
			}
		}
		return false;
	}


	public int getCurrentvalue() {
		return currentvalue;
	}


	public String getProperty() {
		return property;
	}


	public String getFacetvalue() {
		return facetvalue;
	}


	public String getFacetname() {
		return facetname;
	}
}
